package org.example.everyday;

import java.awt.geom.AffineTransform;
import java.beans.DesignMode;
import java.util.*;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2022/2/21 9:42
 */
public class Lc838 {

    public static void main(String[] args) {
        Lc838 lc838 = new Lc838();
        String dominoes = ".L.R...LR..L..";
        String result = lc838.pushDominoes(dominoes);
        System.out.println(result);

        //.L.R....LR..L.."
        //.L.RRRLLLRRLL.."
    }

    /**
     * 输入：dominoes = ".L.R...LR..L.."
     * 输出："LL.RR.LLRRLL.."
     * @param dominoes
     * @return
     */
    public String pushDominoes(String dominoes) {
       int n = dominoes.length();
       Deque<Integer> queue = new ArrayDeque<>();

       int[] time = new int[n];
       Arrays.fill(time,-1);

       List<Character>[] force = new List[n];
        for (int i = 0; i < n; i++) {
            force[i] = new ArrayList<>();
        }
        for (int i = 0; i < n; i++) {
           char f = dominoes.charAt(i);
           if(f != '.'){
               force[i].add(f);
               time[i] = 0;
               queue.offer(i);
           }
        }

        char[] res = new char[n];
        Arrays.fill(res,'.');

        while (!queue.isEmpty()){
            int i = queue.poll();
            if(force[i].size() == 1){
                char f = force[i].get(0);
                res[i] = f;
                int ni = f == 'L'? i - 1:i + 1;
                if(ni >= 0 && ni < n){
                    int t = time[i];
                    if(time[ni] == -1){
                        time[ni] = t + 1;
                        queue.offer(ni);
                        force[ni].add(f);
                    }else if(time[ni] == t + 1){
                        force[ni].add(f);
                    }
                }
            }
        }
        return new String(res);
    }


}
